//
//  KKLabel.m
//  KKDoctor
//
//  Created by Mac mini on 15/10/23.
//  Copyright © 2015年 liao. All rights reserved.
//

#import "KKLabel.h"

#import <CoreText/CoreText.h>

@implementation KKLabel

#pragma mark - Public methods
- (void)setAttributedTextWithText:(NSString*)text {
    
    CGFloat width = CGRectGetWidth([[UIScreen mainScreen] bounds]) - 30;
    [self setAttributedTextWithText: text andWithTextContentWidth: width];
}

- (void)setAttributedTextWithText:(NSString*)text andWithTextContentWidth:(CGFloat)width {
    
    [self setAttributedTextWithText: text andWithTextContentWidth: width andWithLineSpace: 1.0];
}

- (void)setAttributedTextWithText:(NSString *)text andWithTextContentWidth:(CGFloat)width andWithLineSpace:(CGFloat)lineSpace {
    
    if (![text isKindOfClass: [NSString class]]) {
        return;
    }
    
    UIFont* textFont = [UIFont fontWithName: self.font.fontName size: self.font.pointSize + lineSpace];
    CGSize titleSize = CGSizeZero;
    //摘要-计算高度
    {
        CGSize maxSize = CGSizeMake(width, 99999);
        titleSize = [text boundingRectWithSize: maxSize options: NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin attributes: @{NSFontAttributeName: textFont} context:nil].size;
    }
    
    NSMutableAttributedString* attributedString = [[NSMutableAttributedString alloc] initWithString: text];
    // 字间距
    long number = 1.2f;
    CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberLongType,&number);
    [attributedString addAttribute:(id)kCTKernAttributeName value:(__bridge id)num range:NSMakeRange(0,[attributedString length])];
    CFRelease(num);
    // 行间距
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing: lineSpace];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attributedString length])];
    
    self.frame = CGRectMake(CGRectGetMinX(self.frame), CGRectGetMinY(self.frame), titleSize.width, titleSize.height);
    self.attributedText = attributedString;
}

#pragma mark - Private methods
- (void)drawTextInRect:(CGRect)rect {
    
    CGRect contentRect = [self textRectForBounds: rect limitedToNumberOfLines: self.numberOfLines];
    [super drawTextInRect: contentRect];
}

- (void)setContentAlignment:(KKLabelContentAlignment)contentAlignment {
    
    _contentAlignment = contentAlignment;
    [self setNeedsDisplay];
}

- (void)setText:(NSString *)text {
    [super setText: text];
    
    [self drawTextInRect: [self bounds]];
}

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
    
    CGRect textRect = [super textRectForBounds: bounds limitedToNumberOfLines: numberOfLines];
    
    switch (_contentAlignment) {
        case KKLabelContentAlignmentMiddle:
            textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0;
            break;
        case KKLabelContentAlignmentTop:
            textRect.origin.y = bounds.origin.y;
            break;
        case KKLabelContentAlignmentBottom:
            textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;
            break;
            
        default:
            break;
    }
    return textRect;
}

@end
